package biznussoft.metercor.fragments;

import java.util.HashMap;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TextView;
import biznussoft.metercor.HomeActivity;
import biznussoft.metercor.R;
import biznussoft.metercor.listeners.GlobalListener;
import biznussoft.metercor.lookup.SitesLookup;
import biznussoft.metercor.model.Bean_Class;
import biznussoft.metercor.model.StockTransferModel;
import biznussoft.metercor.utils.Session;

import com.salesforce.androidsdk.app.SalesforceSDKManager;
import com.salesforce.androidsdk.rest.RestClient;
import com.salesforce.androidsdk.rest.RestRequest;
import com.salesforce.androidsdk.rest.RestResponse;
import com.salesforce.androidsdk.rest.RestClient.AsyncRequestCallback;
import com.salesforce.androidsdk.util.EventsObservable;
import com.salesforce.androidsdk.util.EventsObservable.EventType;

public class StockTransferDetails  extends Fragment implements OnClickListener {

	private Button save;
	private Bundle args;
	private RestClient client;
	private TextView stockTransferNo,sourceLocation,destinationLocation,additionalInformation,stocktransferno;
	private TableLayout viewLayout,editLayout;
	private EditText etAdditionalInformation;
	public static EditText etSourceLocation,etDestinationLocation; 
	private StockTransferModel bean ;
	private GlobalListener listener;
	String id;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.stock_transfer_details,null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		stockTransferNo = (TextView) getView().findViewById(R.id.tvStocktransferno);		
		sourceLocation = (TextView) getView().findViewById(R.id.tvSourceLocation);
		destinationLocation = (TextView) getView().findViewById(R.id.tvDestinationLocation);
		additionalInformation = (TextView) getView().findViewById(R.id.tvAdditionalInformation);
		stocktransferno = (TextView) getView().findViewById(R.id.tvStocktransferno123);
		etAdditionalInformation = (EditText) getView().findViewById(R.id.additionalInformation);
		etSourceLocation = (EditText) getView().findViewById(R.id.sourceLocation);
		etSourceLocation.setOnClickListener(this);
		etDestinationLocation = (EditText) getView().findViewById(R.id.destinationLocation);
		etSourceLocation.setOnClickListener(this);
		save = (Button) getView().findViewById(R.id.save);
		save.setOnClickListener(this);
		editLayout = (TableLayout) getView().findViewById(R.id.ttLayout1);
		viewLayout = (TableLayout) getView().findViewById(R.id.ttLayout2);

		viewLayout.setVisibility(View.VISIBLE);

		client = HomeActivity.mActivity.client;
		args = getArguments();
		if (args != null) {

			displayStockTransferDetails(args.getInt("Position"));

		}

	}
	
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		listener = (GlobalListener) getActivity();
	}


	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
	}
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu, inflater);


		MenuItem actionItem = menu.add("Logout");                 
		MenuItemCompat.setShowAsAction(actionItem,MenuItemCompat.SHOW_AS_ACTION_ALWAYS | MenuItemCompat.SHOW_AS_ACTION_WITH_TEXT);
		actionItem.setIcon(R.drawable.logout);
		MenuItem newItem = menu.add("Edit");                 
		MenuItemCompat.setShowAsAction(newItem,MenuItemCompat.SHOW_AS_ACTION_ALWAYS | MenuItemCompat.SHOW_AS_ACTION_WITH_TEXT);
		newItem.setIcon(R.drawable.edit); 
		MenuItem stock = menu.add("StockTransferLine");                 
		MenuItemCompat.setShowAsAction(stock,MenuItemCompat.SHOW_AS_ACTION_ALWAYS | MenuItemCompat.SHOW_AS_ACTION_WITH_TEXT);
		stock.setIcon(R.drawable.add);


	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		if(item.getTitle().equals("Logout")){
			SalesforceSDKManager.getInstance().logout(getActivity());}
		if(item.getTitle().equals("Edit")){
			
			listener.getSignature("");
			setHasOptionsMenu(false);
			editStockTransfer();
		}
		if(item.getTitle().equals("StockTransferLine")){
			listener.getSignature("");
			setHasOptionsMenu(false);
			editStockTransfer();
		}
		return super.onOptionsItemSelected(item);
	}
	private void displayStockTransferDetails(int position){

		bean = StockTransferFragment.stockList.get(position);
		stockTransferNo.setText(bean.getName());
		sourceLocation.setText(bean.getSourceLocation());
		destinationLocation.setText(bean.getDestinationLocation());
		additionalInformation.setText(bean.getAdditionalInformation());


	}

	public void newStockTransfer(){
		viewLayout.setVisibility(View.GONE);
		editLayout.setVisibility(View.VISIBLE);
	}
	public void editStockTransfer(){
		viewLayout.setVisibility(View.GONE);
		editLayout.setVisibility(View.VISIBLE);
		stocktransferno.setText(bean.getName());
		etAdditionalInformation.setText(bean.getAdditionalInformation());
		etSourceLocation.setText(bean.getSourceLocation());
		etDestinationLocation.setText(bean.getDestinationLocation());
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.sourceLocation:
			SitesLookup lookup = new SitesLookup();
			lookup.getItemLookUp(getActivity(), "");
			break;
		case R.id.destinationLocation:


			break;
		case R.id.save:
			
			newStocktransfer();
			break;

		default:
			break;
		}
	}
	
	public void newStocktransfer(){
		

		String objectType = "FConnect__Stock_Transfer__c";                                                                       
		HashMap<String, Object> fields = new HashMap<String, Object>();
		fields.put("FConnect__Source_Location__c", Session.sSiteId);
		fields.put("FConnect__Destination_Location__c",Session.sSiteName );
		fields.put("FConnect__Additional_Information__c",etAdditionalInformation.getText().toString());
		
                                        
		try {
			RestRequest restRequest = RestRequest.getRequestForCreate(Bean_Class.api_Version,objectType,fields);


			client.sendAsync(restRequest, new AsyncRequestCallback() {
				@Override
				public void onSuccess(RestRequest request, RestResponse result) {
					try {     
						Log.i("TPT","RestResponse is   :"+result);   
//						finishFragment();
					}                                
					catch (Exception e) {
						Log.i("TPT","Exception is   :"+e.getMessage());                      
					}
				}

				@Override
				public void onError(Exception exception) {
					EventsObservable.get().notifyEvent(EventType.RenditionComplete);
				}
			});
		} 
		catch (Exception e) {
			Log.i("NCS","Exception is   :"+e.getMessage());

		}


	}

	/*TextView siteName,truckquantity,t1;
	Button b1,b2;

	GlobalListener listener;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.stock_transfer_details, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);

		siteName = (TextView) getView().findViewById(R.id.tvSiteName);
		truckquantity = (TextView) getView().findViewById(R.id.truckQuatntity);
		t1 = (TextView) getView().findViewById(R.id.t1);
		b1 = (Button) getView().findViewById(R.id.button1);
		b1.setOnClickListener(this);
		b2 = (Button) getView().findViewById(R.id.button2);
		b2.setOnClickListener(this);
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);

		listener = (GlobalListener) getActivity();

	}
	public void getWarehouseDetails(String  title,String qut) {
		// TODO Auto-generated method stub

		siteName.setText(title);
		truckquantity.setText(qut);

		if(title.contains("Truck")){

			b1.setVisibility(View.GONE);
			b2.setVisibility(View.GONE);
			t1.setVisibility(View.GONE);
		}

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

		switch (v.getId()) {
		case R.id.button1:
			listener.getSignature("nothing");
			break;
		case R.id.button2:
			listener.getSignature("Quantity");
			break;

		default:
			break;
		}

	}*/

}
